package com.ss.android.download;

import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class o {
    public static Random a;
    private static final Pattern b;
    private static final Object c;

    static {
        a = new Random(SystemClock.uptimeMillis());
        b = Pattern.compile("attachment;\\s*filename\\s*=\\s*\"([^\"]*)\"");
        c = new Object();
    }

    static boolean a(String arg7, File arg8) {
        int v2;
        int v6 = 3;
        boolean v0 = true;
        try {
            String v3 = new File(arg7).getCanonicalPath();
            String[] v4 = new String[]{arg8.getCanonicalPath(), Environment.getDownloadCacheDirectory().getCanonicalPath(), Environment.getExternalStorageDirectory().getCanonicalPath()};
            v2 = 0;
            while (v2 < v6) {
                if (v3.startsWith(v4[v2])) {
                    return v0;
                }
                ++v2;
            }
        } catch (IOException v0_1) {
            Log.w("SsDownloadManager", "Failed to resolve canonical path: " + v0_1);
            return false;
        }
        return false;
    }

    static String a(String arg12, String arg13, String arg14, String arg15, String arg16, int arg17, long arg18, r arg20) throws StopRequestException {
        String v4_2;
        String v6_1;
        int v4_1;
        String v5;
        long v6 = 0;
        File v4 = null;
        int v9 = 63;
        int v8 = 47;
        if (arg18 < v6) {
            arg18 = v6;
        }

        if (arg17 == 1) {
            v5 = Uri.parse(arg13).getPath();
        } else {
            switch (arg17) {
                case 0: {
                    File v5_1 = new File(arg20.a.getPath() + aa.a);
                    if (!v5_1.isDirectory() && !v5_1.mkdir()) {
                        throw new StopRequestException(492, "unable to create external downloads directory " + v5_1.getPath());
                    }

                    if (arg13 == null || (arg13.endsWith("/"))) {
                        v6_1 = null;
                    } else {
                        if (aa.d) {
                            Log.v("SsDownloadManager", "getting filename from hint");
                        }

                        v4_1 = arg13.lastIndexOf(v8) + 1;
                        if (v4_1 > 0) {
                            v6_1 = arg13.substring(v4_1);
//                            goto label_68;
                        } else {
                            v6_1 = arg13;
                        }
                    }

//                    label_68:
                    if (v6_1 == null && arg14 != null) {
                        v6_1 = o.a(arg14);
                        if (v6_1 != null) {
                            if (aa.d) {
                                Log.v("SsDownloadManager", "getting filename from content-disposition");
                            }

                            v4_1 = v6_1.lastIndexOf(v8) + 1;
                            if (v4_1 <= 0) {
//                                goto label_81;
                            } else {
                                v6_1 = v6_1.substring(v4_1);
                            }
                        }
                    }

//                    label_81:
                    if (v6_1 != null || arg15 == null) {
                        label_130:
                        v4_2 = v6_1;
                    } else {
                        v4_2 = Uri.decode(arg15);
                        if (v4_2 == null) {
                            v4_2 = v6_1;
                        } else if (v4_2.endsWith("/")) {
                            v4_2 = v6_1;
                        } else if (v4_2.indexOf(v9) < 0) {
                            if (aa.d) {
                                Log.v("SsDownloadManager", "getting filename from content-location");
                            }

                            int v6_2 = v4_2.lastIndexOf(v8) + 1;
                            if (v6_2 <= 0) {
//                                goto label_99;
                            } else {
                                v4_2 = v4_2.substring(v6_2);
                            }
                        } else {
                            v4_2 = v6_1;
                        }
                    }

//                    label_99:
                    if (v4_2 == null) {
                        v6_1 = Uri.decode(arg12);
                        if (v6_1 != null && !v6_1.endsWith("/") && v6_1.indexOf(v9) < 0) {
                            int v7 = v6_1.lastIndexOf(v8) + 1;
                            if (v7 > 0) {
                                if (aa.d) {
                                    Log.v("SsDownloadManager", "getting filename from uri");
                                }

                                v4_2 = v6_1.substring(v7);
                            }
                        }
                    }

                    if (v4_2 == null) {
                        if (aa.d) {
                            Log.v("SsDownloadManager", "using default filename");
                        }

                        v4_2 = "downloadfile";
                    }

                    File v10 = v5_1;
                    v5 = o.b(v4_2);
                    v4 = v10;
                    arg20.a(arg17, v5, arg18);
                    return o.a(v5, arg16, arg17, v4);
                }
                default: {
                    throw new IllegalStateException("unexpected value for destination: " + arg17);
                }
            }
        }

        label_11:
        arg20.a(arg17, v5, arg18);
        return o.a(v5, arg16, arg17, v4);
    }

    private static String a(String arg2) {
        try {
            Matcher v0_1 = o.b.matcher(((CharSequence) arg2));
            if (!v0_1.find()) {
                return null;
            }

            String v0_2 = v0_1.group(1);
            return v0_2;
        } catch (IllegalStateException v0) {
        }

        return null;
    }

    private static String a(String arg7, String arg8, int arg9, File arg10) throws StopRequestException {
        String v0_1;
        int v3 = arg7.lastIndexOf(46);
        int v0 = v3 < 0 || v3 < arg7.lastIndexOf(47) ? 1 : 0;
        if (arg9 == 1) {
            if (v0 != 0) {
                v0_1 = "";
            } else {
                v0_1 = arg7.substring(v3);
                arg7 = arg7.substring(0, v3);
            }
        } else if (v0 != 0) {
            v0_1 = o.a(arg8, true);
        } else {
            v0_1 = null;
            if (arg8 != null) {
                String v2 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(arg7.substring(v3 + 1));
                if (v2 != null && (v2.equalsIgnoreCase(arg8))) {
//                    goto label_75;
                } else {
                    v0_1 = o.a(arg8, false);
                    if (v0_1 != null) {
                        if (!aa.d) {
//                        goto label_75;
                        } else {
                            Log.v("SsDownloadManager", "substituting extension from type");
//                            goto label_75;
                        }
                    } else if (!aa.d) {
//                        goto label_75;
                    } else {
                        Log.v("SsDownloadManager", "couldn\'t find extension for " + arg8);
                    }
                }
            }

            label_75:
            if (v0_1 == null) {
                if (aa.d) {
                    Log.v("SsDownloadManager", "keeping extension");
                }

                v0_1 = arg7.substring(v3);
            }

            arg7 = arg7.substring(0, v3);
        }

        boolean v1 = "recovery".equalsIgnoreCase(arg7 + v0_1);
        if (arg10 != null) {
            arg7 = arg10.getPath() + File.separator + arg7;
        }

        if (aa.d) {
            Log.v("SsDownloadManager", "target file: " + arg7 + v0_1);
        }

        Object v2_1 = o.c;
        synchronized (v2_1) {
            String v1_1 = o.a(arg7, v0_1, v1);
            try {
                File v0_4 = new File(v1_1);
                File v3_1 = v0_4.getParentFile();
                if (v3_1 != null && !v3_1.exists()) {
                    v3_1.mkdirs();
                }

                v0_4.createNewFile();
                return v1_1;
            } catch (IOException v0_3) {
                throw new StopRequestException(492, "Failed to create target file " + v1_1, ((Throwable) v0_3));
            }
        }
    }

    private static String a(String arg4, boolean arg5) {
        String v0 = null;
        if (arg4 != null) {
            v0 = MimeTypeMap.getSingleton().getExtensionFromMimeType(arg4);
            if (v0 != null) {
                if (aa.d) {
                    Log.v("SsDownloadManager", "adding extension from type");
                }

                v0 = "." + v0;
            } else {
                if (!aa.d) {
//                    goto label_15;
                } else {
                    Log.v("SsDownloadManager", "couldn\'t find extension for " + arg4);
                }
            }
        }

        label_15:
        if (v0 == null) {
            if (arg4 != null && (arg4.toLowerCase().startsWith("text/"))) {
                if (arg4.equalsIgnoreCase("text/html")) {
                    if (aa.d) {
                        Log.v("SsDownloadManager", "adding default html extension");
                    }

                    v0 = ".html";
                } else {
                    if (!arg5) {
                        return v0;
                    }

                    if (aa.d) {
                        Log.v("SsDownloadManager", "adding default text extension");
                    }
                    v0 = ".txt";
                }

                return v0;
            }

            if (!arg5) {
                return v0;
            }

            if (aa.d) {
                Log.v("SsDownloadManager", "adding default binary extension");
            }

            v0 = ".bin";
        }

        return v0;
    }

    private static String a(String arg7, String arg8, boolean arg9) throws StopRequestException {
        int v1 = 1;
        String v0 = arg7 + arg8;
        if ((new File(v0).exists()) || (arg9)) {
            String v4 = arg7 + "-";
            int v3 = 1;
            while (v3 < 1000000000) {
                int v2 = v1;
                v1 = 0;
                while (true) {
                    if (v1 < 9) {
                        v0 = v4 + v2 + arg8;
                        if (new File(v0).exists()) {
                            if (aa.d) {
                                Log.v("SsDownloadManager", "file with sequence number " + v2 + " exists");
                            }

                            v2 += o.a.nextInt(v3) + 1;
                            ++v1;
                            continue;
                        }
                    } else {
                        break;
                    }

                    return v0;
                }

                v3 *= 10;
                v1 = v2;
            }

            throw new StopRequestException(492, "failed to generate an unused filename on internal download storage");
        }

        return v0;
    }

    private static String b(String arg6) {
        StringBuffer v3 = new StringBuffer();
        int v0 = 0;
        int v2 = 0;
        while (v0 < arg6.length()) {
            char v4 = arg6.charAt(v0);
            if ((v4 < 0 || v4 > 31) && (v4 != 34 && v4 != 42 && v4 != 47 && v4 != 58 && v4 != 60 && v4 != 62 && v4 != 63 && v4 != 92 && v4 != 124 && v4 != 127)) {
                v3.append(v4);
                v2 = 0;
            } else if (v2 == 0) {
                v3.append('_');
                v2 = 1;
            }

            ++v0;
        }

        return v3.toString();
    }
}

